package jinyilw.swing.rlaf.ui;

import jinyilw.swing.FontTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.utils.RLafUtils;
import jinyilw.swing.visuals.text.JTextCompFunctionListener;

import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTextFieldUI;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author Jinyi
 */
public class RTextFieldUI extends BasicTextFieldUI
{
	JTextField t;
	RFocusManager manager;
	private final RenderingConfig renderingConfig = new RenderingConfig();

	public static ComponentUI createUI(JComponent c)
	{
		return new RTextFieldUI();
	}

	@Override
	public void installUI(JComponent c)
	{
		t = (JTextField) c;
		super.installUI(c);
		c.setFont(FontTools.defaultFont);
		manager = RLafUtils.configTextComponent(t);
	}

	@Override
	protected void installListeners()
	{
		super.installListeners();
		t.addMouseListener(JTextCompFunctionListener.textCompFunctionListener);
	}

	@Override
	protected void uninstallListeners()
	{
		super.uninstallListeners();
		t.removeMouseListener(
				JTextCompFunctionListener.textCompFunctionListener);
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = g2d.getComposite();
		renderingConfig.setAllAAPainting(g2d);
		RLafUtils.paintTextComponent(g, t, manager.isFocus);
		RThemeColor.customTextColorSetting(t);
		paint(g, c);
		renderingConfig.resetAllAAPainting(g2d);
		g2d.setComposite(composite);
	}

	@Override
	protected void paintBackground(Graphics g)
	{

	}

	@Override
	public Dimension getPreferredSize(JComponent c)
	{
		Dimension dim = super.getPreferredSize(c);
		dim.height += 4;
		return dim;
	}
}
